Option Strict Off
Option Explicit On
Module AudioPlayer
	
	Public AudioBasicAudio As QuartzTypeLib.IBasicAudio 'calls to set up Audio media player
	Public AudioMediaEvent As QuartzTypeLib.IMediaEvent
	Public AudioMediaControl As QuartzTypeLib.IMediaControl
	Public AudioMediaPosition As QuartzTypeLib.IMediaPosition
	Dim gRegFilters1 As Object
	Public Sub AudioPlayer_Create(ByRef SoundTrackFile As String) 'Create the audio media player
		Call AudioPlayer_Remove() 'Clean up any previous audio player
		If SoundTrackFile = "No Soundtrack Selected" Then Exit Sub 'No soundtrack has been selected, so exit the audioplayer setup
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(SoundTrackFile) = "" Then Exit Sub 'There is an soundtrack name, but it doesn't exist, so just exit setup.
		
		AudioMediaControl = New QuartzTypeLib.FilgraphManager
		gRegFilters1 = AudioMediaControl.RegFilterCollection 'Gather all the filter names from the system
		
		On Error GoTo NoAudioCodec
		AudioMediaControl.RenderFile((SoundTrackFile)) 'generate the complete graph (linked filters) automatically to suit the file selected by the user above
		
		'   On Error Resume Next
		
		
		AudioMediaPosition = AudioMediaControl 'Enable seeking etc.
		AudioBasicAudio = AudioMediaControl
		If SoundTrackOnOff = True Then 'check if the user wants the audio on
			AudioBasicAudio.Volume = CInt(-10000) 'Set to minimum Volume
			Call AudioPlayer_MaxVolume() 'set sound to maximum
		Else
			AudioBasicAudio.Volume = CInt(0)
			Call AudioPlayer_MinVolume() 'set sound to minimum
		End If
		
		Exit Sub
NoAudioCodec: 
		If SoundTrackFile <> "" Then 'just end if no file has been chosen, but if there are any other errors tell user
			Call MsgBox("I have encountered an error when trying to render the soundtrack file. I will continue without sound", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
		End If
	End Sub
	Public Sub AudioPlayer_Remove() 'Clean up audio media player and variables
		
		On Error GoTo AudioPlayer_RemoveError
		
		
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'

        'If ObjPtr(AudioMediaControl) > 0 Then AudioMediaControl.Stop() 'If a MediaControl instance exists, then stop it from playing
        If Not AudioMediaControl Is Nothing Then AudioMediaControl.Stop() 'If a MediaControl instance exists, then stop it from playing
		'Destroy all objects
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object AudioBasicAudio may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(AudioBasicAudio) > 0 Then AudioBasicAudio = Nothing
        If Not AudioBasicAudio Is Nothing Then AudioBasicAudio = Nothing
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object AudioMediaControl may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(AudioMediaControl) > 0 Then AudioMediaControl = Nothing
        If Not AudioMediaControl Is Nothing Then AudioMediaControl = Nothing
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
		'UPGRADE_NOTE: Object AudioMediaPosition may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(AudioMediaPosition) > 0 Then AudioMediaPosition = Nothing
        If Not AudioMediaPosition Is Nothing Then AudioMediaPosition = Nothing
		Exit Sub
AudioPlayer_RemoveError: 
		Err.Clear()
		Exit Sub
	End Sub
	
	Public Sub AudioPlayer_Play() 'Begin Playing the audio media player
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(AudioMediaControl) <= 0 Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
        If AudioMediaControl Is Nothing Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
		Call AudioPlayer_Sync() 'Syncronse the audio before beginning to play
		AudioMediaControl.Run() 'Play the soundtrack
	End Sub
	
	Public Sub AudioPlayer_Stop() 'Stop playing the audio soundtrack
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(AudioMediaControl) <= 0 Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
        If AudioMediaControl Is Nothing Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
		AudioMediaControl.Pause()
	End Sub
	
	Public Sub AudioPlayer_Sync() 'Syncronise the audio to a specific point
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(AudioMediaControl) <= 0 Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
        If AudioMediaControl Is Nothing Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
		
		
		'Sync the audio with the video, adjusting for the frame offset. (ie delay the soundtrack if required by a number of frames as specified in settings window)
		
		'*****************************Main Form Syncronization***********************************
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(PlayBackMediaControl) <= 0 Then Exit Sub 'If the Main Video is not loaded then , exit subroutine
        If PlayBackMediaControl Is Nothing Then Exit Sub 'If the Main Video is not loaded then , exit subroutine
		
		If ActiveWindowIs = "ButtonWindow" Then 'If we are in the main form syncronise to the playback media player
			
			If (AudioMediaPosition.CurrentPosition > PlayBackMediaPosition.CurrentPosition - SoundTrackAccuracy) And (AudioMediaPosition.CurrentPosition < PlayBackMediaPosition.CurrentPosition + SoundTrackAccuracy) Then Exit Sub 'Check if audio is synced within tolerance
			
			If PlayBackMediaPosition.CurrentPosition + (CDbl(XSheetOffset) / CDbl(FPS)) > AudioMediaPosition.Duration Then 'Check we are not trying to take it past the end of the audio file
				AudioMediaPosition.CurrentPosition = AudioMediaPosition.Duration 'Take the audio as far as it will go
			Else
				AudioMediaPosition.CurrentPosition = PlayBackMediaPosition.CurrentPosition + (CDbl(XSheetOffset) / CDbl(FPS)) 'Syncronise the audio to the main video position
			End If
			Exit Sub
		End If
		
		
		'*****************************Editor Form Syncronization***********************************
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(EditorMediaControl) <= 0 Then GoTo NoEditorMediaPlayer : 'If the Editor Video is not loaded then skip syncronising the rapid player
        If EditorMediaControl Is Nothing Then GoTo NoEditorMediaPlayer 'If the Editor Video is not loaded then skip syncronising the rapid player
		
		If ActiveWindowIs = "EditorWindow" Then 'If we are in the editor form syncronise to the editor media player
			
			If RapidStatus = "Playing" Or RapidStatus = "Looping" Then 'We are in Rapid play mode in the editor
				If (AudioMediaPosition.CurrentPosition > EditorMediaPosition.CurrentPosition - SoundTrackAccuracy) And (AudioMediaPosition.CurrentPosition < EditorMediaPosition.CurrentPosition + SoundTrackAccuracy) Then Exit Sub 'Check if audio is synced within tolerance
				
				If EditorMediaPosition.CurrentPosition + (CDbl(XSheetOffset) / CDbl(FPS)) > AudioMediaPosition.Duration Then 'Check we are not trying to take it past the end of the audio file
					AudioMediaPosition.CurrentPosition = AudioMediaPosition.Duration 'Take the audio as far as it will go
				Else
					AudioMediaPosition.CurrentPosition = EditorMediaPosition.CurrentPosition + (CDbl(XSheetOffset) / CDbl(FPS)) 'Syncronise the audio to the editor video position
				End If
				Exit Sub
			End If
NoEditorMediaPlayer: 
			
			If SlowStatus = "Playing" Or SlowStatus = "Looping" Then 'Check if the slow player is playing or looping
				'  If AudioMediaPosition.CurrentPosition > ((CDbl(EditorListPosition) / CDbl(FPS)) + (CDbl(XSheetOffset) / CDbl(FPS)) - SoundTrackAccuracy) And _
				''     AudioMediaPosition.CurrentPosition < ((CDbl(EditorListPosition) / CDbl(FPS)) + (CDbl(XSheetOffset) / CDbl(FPS)) + SoundTrackAccuracy) Then Exit Sub   'Check if audio is synced within tolerance
				
				If AudioMediaPosition.CurrentPosition = (CDbl(EditorListPosition) / CDbl(FPS)) + (CDbl(XSheetOffset) / CDbl(FPS)) > AudioMediaPosition.Duration Then
					AudioMediaPosition.CurrentPosition = AudioMediaPosition.Duration 'Take the audio as far as it will go
				Else
					AudioMediaPosition.CurrentPosition = (CDbl(EditorListPosition) / CDbl(FPS)) + (CDbl(XSheetOffset) / CDbl(FPS)) 'Syncronise the audio to the main video position
				End If
				Exit Sub
			End If
			
			
			Exit Sub
		End If
		
		
		
	End Sub
	
	Public Sub AudioPlayer_MinVolume() 'Switch the Audio off, ie turn volume to zero
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(AudioMediaControl) <= 0 Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
        If AudioMediaControl Is Nothing Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
		AudioBasicAudio.Volume = -10000 'Set to minimum Volume
	End Sub
	
	Public Sub AudioPlayer_MaxVolume() 'Switch audio on, ie turn volume to maximum
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        If AudioMediaControl Is Nothing Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
		AudioBasicAudio.Volume = 0 'Set to maximum Volume
		
	End Sub
	Public Sub AudioPlayer_SetRate(ByRef PlayRate As Double) 'Set the play rate of the audio player
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        If AudioMediaControl Is Nothing Then Exit Sub 'If the Audio Player hasn't been set up , exit subroutine
		If PlayRate <= 0 Or PlayRate > 1 Then Exit Sub 'Make sure value isn't beyond limits
		AudioMediaPosition.Rate = PlayRate 'Set the rate
	End Sub
End Module